package cn.keepme.redis;

import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;

import cn.keepme.entity.HackerVisitLog;

public abstract class AbstractRedisService<K, V> implements IRedisService<K, V> {

	@Autowired
	private RedisTemplate<K, V> redisTemplate;

	public RedisTemplate<K, V> getRedisTemplate() {
		return redisTemplate;
	}
	public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	
	@Autowired
	private RedisConnectionFactory connectionFactory;
	
	public RedisConnectionFactory getConnectionFactory() {
		return connectionFactory;
	}
	public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}
	/*
	@Autowired
	private RedisTemplate<String, Object> redis;

	public RedisTemplate<String, Object> getRedis() {
		return redis;
	}
	public void setRedis(RedisTemplate<String, Object> redis) {
		redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<HackerVisitLog>(HackerVisitLog.class));  
	    redis.afterPropertiesSet();
		this.redis = redis;
	}
	*/
	/**
	 * 序列化
	 * @param log
	 */
	public void setJacksonSerHackerVisitLog(HackerVisitLog log) {
		RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();
		redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<HackerVisitLog>(HackerVisitLog.class));  
	    redis.afterPropertiesSet();
	    ValueOperations<String, Object> ops = redis.opsForValue();  
	    String key1 = "sn:"+log.getSerial();
	    ops.set(key1,log);
//	    System.out.println(9999);
	}
	
	/**
	 * 反序化
	 * @param log
	 * @return 
	 */
	public HackerVisitLog getJacksonSerHackerVisitLog(String key) {
		RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();
		redis.setConnectionFactory(connectionFactory);  
	    redis.setKeySerializer(ApplicationConfig.StringSerializer.INSTANCE);  
	    redis.setValueSerializer(new JacksonJsonRedisSerializer<HackerVisitLog>(HackerVisitLog.class));  
	    redis.afterPropertiesSet();
	    ValueOperations<String, Object> ops = redis.opsForValue();  
//	    String key = "sn:"+ serial;
        HackerVisitLog hackerVisitLog = new HackerVisitLog();
        hackerVisitLog = (HackerVisitLog)ops.get(key); 
        
//	    System.out.println(hackerVisitLog.getId()+" - "+hackerVisitLog.getSerial());
		return hackerVisitLog;
	}
	
	/**
	 * 添加key value 并且设置存活时间
	 * 
	 * @param key
	 * @param value
	 * @param liveTime
	 */
	@Override
	public void set(final K key, final V value, final long expiredTime) {
		BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
		if (expiredTime <= 0) {
			valueOper.set(value);
		} else {
			valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS);
		}
	}

	@Override
	public V get(final K key) {
		BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key);
		return valueOper.get();
	}

	/**
	 * 通过key删除
	 * 
	 * @param key
	 */
	@Override
	public void del(K key) {
		if (redisTemplate.hasKey(key)) {
			redisTemplate.delete(key);
		}
	}

	@Override
	public Boolean check(K key, V value) {
		Boolean flag = false;
		if (redisTemplate.hasKey(key)) {
			if (value.equals(get(key))) {
				flag = true;
			}
		}
		return flag;
	}
	
	public Boolean check(K key) {
		Boolean flag = false;
		if (redisTemplate.hasKey(key)) {
				flag = true;
		}
		return flag;
	}

}
